from functools import reduce

sum1 = lambda a, b: a + b
print(sum1(2, 3))

sum2 = lambda a, b, c=100: a + b + c
print(sum2(20, 30))
print(sum2(20, 30, 200))

fn1 = lambda *args: args
print(fn1(2, 3, 5))

fn2 = lambda **kwargs: kwargs
print(fn2(name='tom', age=18))

max1 = lambda a, b: a if a >= b else b
print(f'较大值{max1(3, 5)}')
students = [
    {'name': 'tom', 'age': 20},
    {'name': 'a', 'age': 20},
    {'name': 'rose', 'age': 19},
    {'name': 'jack', 'age': 22}
]

students.sort(key=lambda x: x['name'], reverse=True)
print(students)
# sorted(),类型于列表的sort（），他不会改变原列表
print(sorted(students, key=lambda x: x['age'], reverse=True))
# 按value降序排序
dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7}
print(sorted(dict1.items(), key=lambda item: item[1], reverse=True))
print(sorted(dict1.items(), key=lambda item: (item[1], item[0])))


def sum1(a, b, fn):
    return fn(a) + fn(b)


result = sum1(-1, 2, abs)
print(result)

list1 = [1, 2, 3, 4, 5, 6]
li1 = [i ** 2 for i in list1]
print(li1)

result1 = map(lambda a: a ** 2, list1)
print(list(result1))

result2 = map(lambda a: a ** a if a % 2 == 0 else a, list1)
print(list(result2))
li2 = [i ** 2 if i % 2 == 0 else i for i in list1]
print(li2)

# def fun1(a):
#     if a % 2 == 0:
#         a = a * a
#
#
# result = map(fun1(a), list1)

list2 = [1, 2, 3, 4, 5]
r3 = reduce(lambda a, b: a + b, list2)
print(r3)

list3=[1,2,3,4,5,6,7,8,9,10]
r4=filter(lambda a:a if a%2 ==0 else '',list3)
print((list(r4)))